package com.caiheng.api.service.platfom.impl;

import cn.hutool.json.JSONObject;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caiheng.api.dao.platform.ErrorListMapper;
import com.caiheng.api.dao.platform.SmsTemplateMapper;
import com.caiheng.api.entity.estate.EstateRoom;
import com.caiheng.api.entity.platform.ErrorList;
import com.caiheng.api.entity.platform.SmsTemplate;
import com.caiheng.api.entity.wxuser.User;
import com.caiheng.api.service.base.impl.BaseServiceImpl;
import com.caiheng.api.service.platfom.SmsTemplateService;
import com.caiheng.api.util.SendSms;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

@Service
public class SmsTemplateServiceImpl extends BaseServiceImpl<SmsTemplateMapper, SmsTemplate> implements SmsTemplateService {

    @Resource
    private SmsTemplateMapper smsTemplateMapper;
    @Resource
    private ErrorListMapper errorListMapper;

    @Override
    public void sendSmsByProductOrder(String merName, String phoneNum, String productName) {
        //${name}管理员您好，新订单来喽~, 用户下单${product}。感谢您的使用，祝您生意兴隆~
        SmsTemplate sms = smsTemplateMapper.selectOneByColumn("sms_type",1);
        if(sms!=null){
            SendSms sendSms = new SendSms();
            //请求失败这里会抛ClientException异常
            JSONObject smsJson = new JSONObject();
            smsJson.set("name", merName);
            smsJson.set("product", productName);
            try {
                sendSms.send(phoneNum,smsJson,sms);
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void sendSmsByProductOrderVisit(String merName, String phoneNum, String productName) {
        //${name}管理员您好，新订单来喽~, 用户下单${product}，请尽快联系客户处理。感谢您的使用，祝您生意兴隆~
        SmsTemplate sms = smsTemplateMapper.selectOneByColumn("sms_type",2);
        if(sms!=null){
            SendSms sendSms = new SendSms();
            //请求失败这里会抛ClientException异常
            JSONObject smsJson = new JSONObject();
            smsJson.set("name", merName);
            smsJson.set("product", productName);
            try {
                sendSms.send(phoneNum,smsJson,sms);
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public String sendSmsByPhoneCode(String code, String phoneNum, Integer sms_type) {
        //您的验证码为：${code}，请勿泄露于他人！
        QueryWrapper<SmsTemplate> qw = new QueryWrapper<>();
        qw.eq("sms_type",sms_type);
        qw.last("limit 1");
        SmsTemplate sms = smsTemplateMapper.selectOne(qw);
        if(sms != null){
            SendSms sendSms = new SendSms();
            //请求失败这里会抛ClientException异常
            JSONObject smsJson = new JSONObject();
            smsJson.set("code", code);
            try {
                SendSmsResponse response = sendSms.send(phoneNum, smsJson, sms);
                System.out.println("发送短信：手机号： " + phoneNum + ": " + response.getMessage().toString());
                if(!response.getMessage().toString().equals("OK")){
                    ErrorList errorList = new ErrorList();
                    errorList.setApiUrl("");
                    errorList.setErrorType("sms");
                    errorList.setFromType("api");
                    errorList.setTitle("手机号：" +phoneNum + "，发送验证码失败");
                    errorList.setCreateTime(new Date());
                    errorList.setErrorContent(response.getMessage().toString());
                    errorListMapper.insert(errorList);
                }
                return response.getCode();
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }
        return "FAIL";
    }


    @Override
    public void sendSmsByPropertyFeeToRooUser(String phoneNum, EstateRoom room, String year, String month1, String month2, BigDecimal money, String telephone) {
        //${name}管理员您好，新订单来喽~, 用户下单${product}。感谢您的使用，祝您生意兴隆~
        SmsTemplate sms = smsTemplateMapper.selectOneByColumn("sms_type",3);
        if(sms!=null){
            SendSms sendSms = new SendSms();
            //请求失败这里会抛ClientException异常
            JSONObject smsJson = new JSONObject();
            smsJson.set("address", room.getRoomNo());
            smsJson.set("year", year);
            smsJson.set("month1", month1);
            smsJson.set("month2", month2);
            smsJson.set("money", money);
            smsJson.set("telephone", telephone);
            try {
                sendSms.send(phoneNum,smsJson,sms);
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }
    }

}
